﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;

namespace NntpTransport
{
	/// <summary>
	/// Channel
	/// Evaluate the chance to derive from ConnectionOrientedTransportBindingElement
	/// </summary>
	public class NntpTransportBindingElement : TransportBindingElement
	{
		public bool Streamed;

		public NntpTransportBindingElement()
		{
		}

		public NntpTransportBindingElement(NntpTransportBindingElement other)
			: base(other)
		{
			this.Streamed = other.Streamed;
		}

		public override string Scheme
		{
			get { return NntpUriHelper.UriScheme; }
		}

		public override BindingElement Clone()
		{
			return new NntpTransportBindingElement(this);
		}

		public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
		{
			return typeof(TChannel) == typeof(IRequestChannel);
			//return base.CanBuildChannelFactory<TChannel>(context);
		}

		public override bool CanBuildChannelListener<TChannel>(BindingContext context)
		{
			return typeof(TChannel) == typeof(IReplyChannel);
			//return base.CanBuildChannelListener<TChannel>(context);
		}

		public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
		{
			if(context == null)
				throw new ArgumentException("BuildChannelFactory: context is null");
			if(!CanBuildChannelFactory<TChannel>(context))
				throw new ArgumentException(string.Format("This type of channel is not supported: {0}", typeof(TChannel).Name));
			return (IChannelFactory<TChannel>)(object)new NntpRequestChannelFactory(this, context);

			//return base.BuildChannelFactory<TChannel>(context);
		}

		public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
		{
			if(context == null)
				throw new ArgumentException("BuildChannelFactory: context is null");
			if(!CanBuildChannelListener<TChannel>(context))
				throw new ArgumentException(string.Format("This type of channel is not supported: {0}", typeof(TChannel).Name));
			return (IChannelListener<TChannel>)(object)new NntpReplyChannelListener(this, context);

			//return base.BuildChannelListener<TChannel>(context);
		}
	}
}
